package com.cll.learn.consumer;

import com.cll.learn.consumer.agent.AgentDemoService;
import com.cll.learn.consumer.task.ConsoleExecuteNumberTask;
import com.cll.learn.monitor.thread.BaseThreadFactory;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author CLL
 */
@EnableDubbo(scanBasePackages = {"com.cll.learn"})
@SpringBootApplication
public class ServiceConsumerApplication {

    private static final Logger LOGGER = LoggerFactory.getLogger(ServiceConsumerApplication.class);

    public static final AtomicLong EXECUTE_NUMBER = new AtomicLong(0);



    public static void main(String[] args) {
        LOGGER.info("消费端开始启动");
        ApplicationContext context = SpringApplication.run(ServiceConsumerApplication.class, args);
        LOGGER.info("消费端启动成功");
        AgentDemoService bean = context.getBean(AgentDemoService.class);

        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1, new BaseThreadFactory("monitor-group", "monitor-thread"));
        scheduledThreadPoolExecutor.scheduleAtFixedRate(new ConsoleExecuteNumberTask(), 10, 10, TimeUnit.SECONDS);
        while (true) {
//            try {
//                Thread.sleep(500);
                long l = EXECUTE_NUMBER.incrementAndGet();
                bean.agentMethodA("cll");
                bean.agentMethodB("yl");
                bean.agentMethodC("cy");
                LOGGER.info("===>>> 提示消息,执行次数：" + l);
//            } catch (InterruptedException ie) {
//                LOGGER.error("****** 服务端主线程线程休眠异常" + ie.getMessage(), ie);
//            }
        }
    }

}
